home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cserial.zip / DELAY.C < prev    next >
Text File  |  1990-04-04  |  2KB  |  75 lines

  1. /*
  2.  *                               DELAY.C
  3.  *
  4.  *                           Written for the
  5.  *
  6.  *                              Datalight
  7.  *                           Microsoft V 5.x
  8.  *                                TurboC
  9.  *                                  &
  10.  *                               Zortech
  11.  *
  12.  *                             C Compilers
  13.  *
  14.  *            Copyright (c) John Birchfield 1987, 1988, 1989
  15.  *
  16.  *    This module implements a more or less machine independent delay
  17.  *    loop - the general idea is we kinda tie the delay loop to the 
  18.  *    Timer Interrupt to get a general idea of how big the loop counter
  19.  *    has to be to count off a millisecond.  Crude eh?
  20.  */
  21.  
  22. /*
  23.  *    We'll take the stand-alone testing stuff out entirely
  24.  *    one of these days.
  25.  */
  26.  
  27. unsigned long int timer_read ();
  28.  
  29. #if (defined (TEST))
  30. #    include <stdio.h>
  31. #    include <time.h>
  32. main ()
  33. {
  34.     long    starttime, stoptime;
  35.     DELAY_init ();
  36.     printf ("\n\nBegin %d millisecond delay\n\n", 30000);
  37.     time (&starttime);
  38.     DELAY_loop (30000);
  39.     time (&stoptime);
  40.     printf ("End - Elapsed time = %ld\n", stoptime - starttime);
  41.     timer_term ();
  42. }
  43.  
  44. #endif
  45.  
  46.  
  47. static unsigned int DELAY_millisecond;
  48. void
  49. DELAY_init (void)
  50. {
  51.     static d_init = 0;
  52.     long    l;
  53.     if (d_init)
  54.         return;
  55.     d_init = 0;
  56.     timer_init ();
  57.     timer_set ();
  58.     while (timer_read () == 0L);
  59.     timer_set ();
  60.     for (l = 0L; l != -1L; l++)
  61.         if (timer_read () == 3)
  62.             break;
  63.     l *= 197;
  64.     DELAY_millisecond = (int) ((l + 5000) / 10000);
  65. }
  66. static unsigned int _DELAY_cnt, _DELAY_ix;
  67.  
  68. int 
  69. DELAY_loop (int msc)
  70. {
  71.     for (_DELAY_ix = 0; _DELAY_ix < msc; _DELAY_ix++)
  72.         for (_DELAY_cnt = 0; _DELAY_cnt < DELAY_millisecond; _DELAY_cnt++);
  73.     return (msc);
  74. }
  75.